﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CodeThisSite.Models;
using HtmlAgilityPack;

namespace CodeThisSite.Controllers
{
    public class HtmlController : ApiController
    {
        List<Mission> basicMissions = new List<Mission>()
        {
            new Mission("Hello Internet", 1, "<h1>Hello World</h1>"),
            new Mission("WebMAster - Paragraph", 2, "<p>Hi, I am Web master!</p>"),
            new Mission("Writing Articles!", 3, "<article><p>This is the first paragraph!</p><p>This is the second paragraph!</p></article>"),
            new Mission("Link", 4, "<a href=\"http://google.com\">Google</a>"),
            new Mission("Image", 5, "<img href=\"computer.jpg\">")
        };
        List<Mission> realisticMissions = new List<Mission>() 
        {
            new Mission("Input box - username", 1, "<input type=\"text\" name=\"username\">"),
            new Mission("Input box - password", 2, "<input type=\"password\" name=\"password\">"),
            new Mission("Forms - login", 3, "Username: <input type=\"text\" name=\"username\"><br> Password: <input type=\"password\" name=\"password\">"),
            new Mission("Navigation", 4, "<nav><ul><li><a href=\"index.html\">Home</a></li><li><a href=\"about.html\">About</a></li><ul></nav>"),
            new Mission("A whole new level - Radio box ", 5, "<input type=\"radio\" name=\"sex\" value=\"male\"> Male<br><input type=\"radio\" name=\"sex\" value=\"female\"> Female")
        };
        string html = "";
        // GET api/<controller>

        [HttpGet]
        public void Initialize(string _params)
        {
            html = _params;
        }

        [HttpGet]
        public string ValidateMission()
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);

            if (doc.ParseErrors.Count() > 0)
            {
                return "error!";
            }
            else
            {
                //validate mission
                if (true)
                {
                    return "OK";
                }
                else
                {
                    return "Nope";
                }
            }
        }

        [HttpPost]
        public string GetTrueMissionAnswer([FromUri]string _params)
        {
            switch (_params)
            {
                //Basics

                case "b1":
                    return basicMissions[0].Answer;
                case "b2":
                    return basicMissions[1].Answer;
                case "b3":
                    return basicMissions[2].Answer;
                case "b4":
                    return basicMissions[3].Answer;
                case "b5":
                    return basicMissions[4].Answer;

                //Realistics

                case "r1":
                    return realisticMissions[0].Answer;
                case "r2":
                    return realisticMissions[1].Answer;
                case "r3":
                    return realisticMissions[2].Answer;
                case "r4":
                    return realisticMissions[3].Answer;
                case "r5":
                    return realisticMissions[4].Answer;
                default:
                    return "Mission not found";
            }
        }

    }
}